# -*- coding: utf-8 -*-
'''
Created on 2013-4-12

@author: dyq
'''
from Cms.plus import user 
from Common import htmlHlep
from Cms import config
#拦截器  http://zhbmblog.appspot.com/?p=12001
#如果没有参数可以不要这层 key输出参数
def page_render(userGrade):
   
    #view=被拦截方法
    def render(view):
        def func(request, *arg):
            userbll=user.user()
            
            if userbll.IsLogin(request) & userbll.Detect(request, userGrade):
               
                if config.Config().IsDevelop:
                    return view(request, *arg)
                else:
                    try:
                        return view(request, *arg)
                    except Exception,ex:
                        return  __ErrorHandle__(request,ex)
                    
            else:
                return __TimeOutHandle__(request)
                
        return func
    return render

def __ErrorHandle__(request,ex):
    if request.is_ajax()==False:
        return htmlHlep.Find.Redirect("/AdminMain/Home/Login")
    else:
        print ex
        obj={"IsError":True,"ErrorTxt":"调用异常"}
        return htmlHlep.Find.Json(obj)

def __TimeOutHandle__(request):
    if request.is_ajax()==False:
        return htmlHlep.Find.Redirect("/AdminMain/Home/Login")
    else:
        obj={"IsError":True,"ErrorTxt":"丢失登录状态"}
        return htmlHlep.Find.Json(obj)

def __IsAjax__(request):
    return  'HTTP_X_REQUESTED_WITH' in request.META#&'XMLHttpRequest'== request.META['X-Requested-With']
       



